package com.foreknow.web;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.Constanttype;
import com.foreknow.service.ConstanttypeService;
import com.foreknow.utils.Constance;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("constanttype")
public class ConstanttypeController {
    @Autowired
    private ConstanttypeService constanttypeService;

    @RequestMapping(value = "/findConstanttypeByPage",method = RequestMethod.POST)
    @ResponseBody
    public String findConstanttypeByPage(@RequestBody Map<String,Object> param) throws JsonProcessingException {
        //设置分页
        int pagenum = (int) param.get("pagenum");
        int pagesize = (int) param.get("pagesize");
        PageHelper.startPage(pagenum,pagesize);
        //获取常数类别列表
        String query = (String) param.get("query");
        List<Constanttype> list =  constanttypeService.searchConstantTypeList(query);
        PageInfo<Constanttype> info = new PageInfo<>(list);
        //定义储存信息的集合
        HashMap<String,Object> hs = new HashMap<>();
        if (list != null){
            if (query != null && !query.equals("")){
                hs.put("data",info);
                hs.put("meta",new Constance("查询常数类别成功",200));
            }else{
                hs.put("data",info);
                hs.put("meta",new Constance("获取常数类别列表成功",200));
            }
        }else{
            if (query != null && !query.equals("")){
                hs.put("data",info);
                hs.put("meta",new Constance("没有匹配的常熟类别,请重新输入",200));
            }
            hs.put("meta",new Constance("获取常数类别列表失败",201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/addConstanttype",method = RequestMethod.POST)
    @ResponseBody
    public String addConstanttype(@RequestBody Constanttype constanttype) throws JsonProcessingException {
        int x = constanttypeService.insertConstantType(constanttype);
        HashMap<String, Object> hs = new HashMap<>();
        if (x == 1) {
            hs.put("meta", new Constance("添加常数类别成功", 200));
        } else {
            hs.put("meta", new Constance("添加常数类别失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/selectConstanttype",method = RequestMethod.GET)
    @ResponseBody
    public String selectConstanttypeById(@RequestParam Map<String,Object> param) throws JsonProcessingException {
        String id = (String) param.get("id");
        Constanttype constanttype = constanttypeService.searchConstantType(Integer.parseInt(id));
        HashMap<String,Object> hs = new HashMap<>();
        if (constanttype != null){
            hs.put("data",constanttype);
            hs.put("meta",new Constance("获取常数类别成功",200));
        }else{
            hs.put("meta",new Constance("获取常数类别失败",201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/updateConstanttype",method = RequestMethod.POST)
    @ResponseBody
    public String updateConstanttypeByObject(@RequestBody Constanttype constanttype) throws JsonProcessingException {
        int x = constanttypeService.updateConstantType(constanttype);
        HashMap<String, Object> hs = new HashMap<>();
        if (x == 1) {
            hs.put("meta", new Constance("修改常数类别成功", 200));
        } else {
            hs.put("meta", new Constance("修改常数类别失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/deleteConstanttype",method = RequestMethod.GET)
    @ResponseBody
    public String deleteConstanttypeByDelMark(@RequestParam Integer id) throws JsonProcessingException {
        int x = constanttypeService.deleteConstantType(id);
        HashMap<String, Object> hs = new HashMap<>();
        if (x == 1) {
            hs.put("meta", new Constance("删除常数类别成功", 200));
        } else {
            hs.put("meta", new Constance("删除常数类别失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
}
